package vn.student.mylib;

import java.util.ArrayList;
import vn.student.R;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class ListClassSTAdapter extends ArrayAdapter<ClassInfoST>{
	Activity act;
	ArrayList<ClassInfoST> list;
	public ListClassSTAdapter(Activity act,ArrayList<ClassInfoST> list) {
		super(act, R.layout.class_row_detail,list);
		this.act=act;
		this.list = list;
	}
	public View getView(int position, View convertView,ViewGroup parent) {
		 View row=convertView;
		 RowWrapper wrapper=null;
	     if (row==null) {                         
	    	 LayoutInflater inflater=act.getLayoutInflater();
	    	 row=inflater.inflate(R.layout.class_row_detail, null);
	    	 wrapper=new RowWrapper(row);
	    	 row.setTag(wrapper);
	     }
	     else {
	    	 wrapper=(RowWrapper)row.getTag();
	     }
	     ClassInfoST tmp = list.get(position);
	     wrapper.getTypeLabel().setText(ClassType.getName(tmp.type));
	     wrapper.getClassNameLabel().setText(tmp.className);
	     wrapper.getlocationLabel().setText(tmp.location);
	     wrapper.getAssCountLabel().setText(tmp.assCount+" Assignement");
	     wrapper.getTimeLabel().setText(tmp.timeStart.substring(0, 5)+"-"+tmp.timeEnd.substring(0, 5));
	     //wrapper.getIcon().setImageResource(R.drawable.icon);
	     //wrapper.getClassIcon().setImageResource(R.drawable.icon);
	     wrapper.getIcon().setImageResource(ClassType.getIconId(tmp.type));
	     if(list.size()==1){
    		 row.setBackgroundResource(R.drawable.class_row_all);
    	 }
    	 else{
    		 if(position==(list.size()-1))
    			 row.setBackgroundResource(R.drawable.class_row_bottom);
    		 else 
    			 if(position==0)
    				 row.setBackgroundResource(R.drawable.class_row_top);
    			 else
    				 row.setBackgroundResource(R.drawable.class_row_none);
    	 }
	     return(row);
	}
	public ArrayList<ClassInfoST> getClassInfoList(){
		return list;
	}
	public void setClassInfoList(ArrayList<ClassInfoST> list){
		this.list = list;
	}
	
	private class RowWrapper {
		View base;
		TextView typeLabel = null;
		TextView classNameLabel = null;
		TextView timeLabel=null;
		TextView locationLabel=null;
		TextView assCountLabel=null;
		ImageView classIcon = null;
		
		ImageView getIcon() {
			if (classIcon==null) {
				classIcon=(ImageView)base.findViewById(R.id.classIcon);
			}
			return(classIcon);
		}
	  
	  
		RowWrapper(View base) {
			this.base=base;
		}
		TextView getTypeLabel() {
			if (typeLabel==null) {
				typeLabel=(TextView)base.findViewById(R.id.typeLabel);
			}
		  return(typeLabel);
		}
		TextView getClassNameLabel() {
			if (classNameLabel==null) {
				classNameLabel=(TextView)base.findViewById(R.id.classNameLabel);
			}
		  return(classNameLabel);
		}
		TextView getTimeLabel() {
			if (timeLabel==null) {
				timeLabel=(TextView)base.findViewById(R.id.timeLabel);
			}
		  return(timeLabel);
		}
		TextView getlocationLabel() {
			if (locationLabel==null) {
				locationLabel=(TextView)base.findViewById(R.id.locationLabel);
			}
		  return(locationLabel);
		}
		TextView getAssCountLabel() {
			if (assCountLabel==null) {
				assCountLabel=(TextView)base.findViewById(R.id.assCountLabel);
			}
		  return(assCountLabel);
		}
	}
}
